﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Pan
{
    public partial class file : System.Web.UI.Page
    {
        protected string code;
        protected string uid;
        protected string uname;
        protected string sharename;
        protected string sharefids;
        protected int path;
        protected string breadcrumb;
        protected void Page_Load(object sender, EventArgs e)
        {
            // 参数检查
            this.code = Request.QueryString["code"];
            if (this.code == "" || this.code == null)
            {
                return;
            }
            try
            {
                this.path = Convert.ToInt32(Request.QueryString["path"]);
            }
            catch(Exception err)
            {
                this.path = -1;
            }
            if (this.path < 0)
            {
                Response.Redirect("/file.aspx?code=" + this.code);
                return;
            }
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["PanConnectionString"].ToString();
            try
            {
                conn.Open();
                SqlCommand cmdShare = new SqlCommand();
                cmdShare.Connection = conn;
                cmdShare.CommandType = CommandType.StoredProcedure;
                cmdShare.CommandText = "GetShare";
                cmdShare.Parameters.Add("@code", SqlDbType.VarChar).Value = this.code;
                SqlDataReader sdr = cmdShare.ExecuteReader();
                if (sdr.Read())
                {
                    this.uid = sdr["uid"].ToString();
                    this.uname = sdr["uname"].ToString();
                    this.sharename = sdr["name"].ToString();
                    this.sharefids = sdr["fids"].ToString();
                    sdr.Close();
                    if (this.path == 0)
                    {
                        this.breadcrumb = "<a><cite>分享根目录</cite></a>";
                        SqlDataSource.SelectParameters["fids"].DefaultValue = this.sharefids;
                    }
                    else
                    {
                        // 获取请求path的目录树信息
                        SqlCommand cmdParent = new SqlCommand();
                        cmdParent.Connection = conn;
                        cmdParent.CommandType = CommandType.StoredProcedure;
                        cmdParent.CommandText = "GetFileParent";
                        cmdParent.Parameters.Add("@id", SqlDbType.Int).Value = this.path;
                        cmdParent.Parameters.Add("@uid", SqlDbType.Int).Value = int.Parse(this.uid);
                        SqlDataReader sdrp = cmdParent.ExecuteReader();
                        List<string> pathIdList = new List<string>();
                        List<string> pathNameList = new List<string>();
                        while (sdrp.Read())
                        {
                            pathIdList.Add(sdrp["id"].ToString());
                            pathNameList.Add(sdrp["name"].ToString());
                        }
                        sdrp.Close();
                        // 判断请求的path是否在分享范围，并存储目录层级信息
                        pathIdList.Reverse();
                        string[] pathIdArr = pathIdList.ToArray();
                        string[] pathNameArr = pathNameList.ToArray();
                        List<string> sharefidList = this.sharefids.Split(',').ToList();
                        List<string[]> breadList = new List<string[]>();
                        bool flag = false;
                        int j = pathNameArr.Length - 1;
                        for (int i = 0; i < pathIdArr.Length; i++, j--)
                        {
                            breadList.Add(new string[2] { pathIdArr[i], pathNameArr[j] });
                            if (sharefidList.IndexOf(pathIdArr[i]) != -1)
                            {
                                flag = true;
                                break;
                            }
                        }
                        if (flag)
                        {
                            // 计算面包屑
                            this.breadcrumb = "<a href=\"/file.aspx?code=" + this.code + "\">分享根目录</a>";
                            int i = 0;
                            breadList.Reverse();
                            foreach (string[] s in breadList)
                            {
                                if (i < breadList.Count - 1)
                                    this.breadcrumb += "<a href=\"/file.aspx?code=" + this.code + "&path=" + s[0] + "\">" + s[1] + "</a>";
                                else
                                    this.breadcrumb += "<a><cite>" + s[1] + "</cite></a>";
                                i++;
                            }
                            // 获取子目录
                            List<string> l = new List<string>();
                            SqlCommand cmdList = new SqlCommand();
                            cmdList.Connection = conn;
                            cmdList.CommandType = CommandType.StoredProcedure;
                            cmdList.CommandText = "GetFileList";
                            cmdList.Parameters.Add("@pid", SqlDbType.Int).Value = this.path;
                            cmdList.Parameters.Add("@uid", SqlDbType.Int).Value = int.Parse(uid);
                            SqlDataReader sdrl = cmdList.ExecuteReader();
                            while (sdrl.Read())
                            {
                                l.Add(sdrl["id"].ToString());
                            }
                            sdrl.Close();
                            SqlDataSource.SelectParameters["fids"].DefaultValue = string.Join(",", l.ToArray());
                        }
                        else
                        {
                            Response.Redirect("/file.aspx?code=" + this.code);
                        }
                    }
                }
                else
                {
                    Session["error"] = "分享找不到了";
                    Response.StatusCode = 404;
                }
            }
            catch (Exception err)
            {
                Master.RespLabel.Text = "<script>layui.use('layer',function(){layui.layer.alert('" + err.Message + "')})</script>";
            }
            finally
            {
                conn.Close();
            }
        }
    }
}